﻿using System.Collections.Generic;
using System.Linq;
using ChiWalker.Settings;
using ChiWalker.Spell;
using Styx;
using Styx.TreeSharp;
using Styx.WoWInternals.WoWObjects;

namespace ChiWalker.Helpers
{
    internal static class Group
    {
        public static List<WoWUnit> PartyMembersCache = new List<WoWUnit>();


        internal static IEnumerable<WoWPartyMember> GroupMembers
        {
            get
            {
                return !StyxWoW.Me.GroupInfo.IsInRaid
                    ? StyxWoW.Me.GroupInfo.PartyMembers
                    : StyxWoW.Me.GroupInfo.RaidMembers;
            }
        }

        public static void UpdatePartyCache()
        {
            PartyMembersCache.Clear();
            //Make Sure were in the list


            foreach (WoWPartyMember unit in GroupMembers)
            {
                WoWPlayer player = unit.ToPlayer();
                if (player == null) continue;
                PartyMembersCache.Add(player);
            }

            if (!PartyMembersCache.Contains(StyxWoW.Me))
                PartyMembersCache.Add(StyxWoW.Me);
        }

        private static bool HasKings(this WoWUnit unit)
        {
            return unit.HasAura("Blessing of Kings") || unit.HasAura("Leader of the Pack") ||
                   unit.HasAura("Legacy of the Emperor") ||
                   unit.HasAura("Mark of the Wild");
        }

        private static bool HasCritbuff(this WoWUnit unit)
        {
            return unit.HasAura("Dalaran Brilliance") ||
                   unit.HasAura("Arcane Brilliance") ||
                   unit.HasAura("Legacy of the White Tiger") ||
                   unit.HasAura("Furious Howl") ||
                   unit.HasAura("Fearless Roar") ||
                   unit.HasAura("Terrifying Roar") ||
                   unit.HasAura("Still Water");
        }

        private static bool HasSpellPowerBuff(this WoWUnit unit)
        {
            return
                unit.HasAura("Arcane Brilliance") ||
                unit.HasAura("Still Water") ||
                unit.HasAura("Burning Wrath") ||
                unit.HasAura("Dark Intent");
        }

        private static bool HasStamina(this WoWUnit unit)
        {
            return unit.HasAura("Power Word: Fortitude") ||
                   unit.HasAura("Commanding Shout") ||
                   unit.HasAura("Qiraji Fortitude");
        }

        public static Composite BuffStam()
        {
            return
                new Decorator(
                    ret =>
                        ChiWalkerSettings.Instance.BuffGroup &&
                        PartyMembersCache.Count(u => u.IsAlive && !u.HasStamina()) >= 1 ||
                        !StyxWoW.Me.HasStamina(),
                    new PrioritySelector(
                        new CastOnSelf(Routines.Priest.Spell.PowerWordFortitude),
                        new CastOnSelf(Routines.Warrior.Spell.CommandingShout)
                        ));
        }

        public static Composite BuffKings()
        {
            return
                new Decorator(
                    ret =>
                        ChiWalkerSettings.Instance.BuffGroup &&
                        PartyMembersCache.Count(u => u.IsAlive && !u.HasKings()) >= 1 ||
                        !StyxWoW.Me.HasKings(),
                    new PrioritySelector(
                        new CastOnSelf(Routines.Paladin.Spell.BlessingofKings),
                        //new CastOnSelf(Routines.Monk.Spell.),
                        new CastOnSelf(Routines.Druid.Spell.MarkoftheWild)
                        )
                    );
        }

        public static Composite BuffCritChance()
        {
            return
                new Decorator(
                    ret =>
                        ChiWalkerSettings.Instance.BuffGroup &&
                        PartyMembersCache.Count(u => u.IsAlive && !u.HasCritbuff()) >= 1 ||
                        !StyxWoW.Me.HasCritbuff(),
                    new PrioritySelector(
                        //new CastOnSelf(Routines.Monk.Spell.m),
                        //new CastOnSelf(Routines.m)
                        )
                    );
        }
    }
}